//获取应用实例
const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    page: 1,
    navList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.id)
    console.log(options.name)
    wx.setNavigationBarTitle({
      title: ''+options.name,
    })
    this.getThemeGoods(options.id);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow: function() {
    var that = this;
  },

  getDeviceInfo: function() {
    let that = this
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },
  

  //主题商品查询
  getThemeGoods(index) {
    wx.showLoading({
      title: '正在加载',
      mask:false
    })
    var that = this;
    var url;
    var sort_type;
    console.log(index)
    url = app.globalData.api + '/app.php?c=Pdd&a=getThemeGoods';
    sort_type = index;
    wx.request({
      url: url,
      data: {
        token: app.globalData.token,
        theme_id: '' + sort_type,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function(res) {
        console.log(res);
        if(res.data.code==0){
          that.setData({
            navList: that.data.navList.concat(res.data.data.goods_list)
          })
        } else {
          if (res.data.code == 105) {
            wx.removeStorage({
              key: 'token',
              success: function (res) {
                console.log(res)
              },
            })
            app.globalData.token = '';
            wx.reLaunch({
              url: '/pages/mine/login',
            })
          }
          wx.showToast({
            title: '' + res.data.msg,
          })
        }
        wx.hideLoading();
      },
      fail: function() {

      }
    });
  },

  goodDetail: function (e) {
    console.log(app.globalData.token)
    if (app.globalData.token != '') {
      console.log(e);
      wx.navigateTo({
        url: '/pages/shop/rx-item?id=' + e.currentTarget.dataset.id,
      })
    } else {
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that=this;
    // that.setData({
    //   page: 1,
    //   navList:[]
    // })
    // that.getAllList(this.data.currtab+1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    // console.log(this.data.currtab + 1)
    that.setData({
      page: that.data.page + 1,
    })
    // that.getAllList(this.data.currtab+1);
  },
});